package com.hbnu.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author kittychen
 * @since 2025-11-05 16:34
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置编码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        // 1、获取用户名和密码
        String email = req.getParameter("email");
        String password = req.getParameter("password");

        // 2、真实账号密码
        String realEmail = "admin@163.com";
        String realPassword = "123456";

        // 3、校验用户名和密码
        if (email.equals(realEmail) && password.equals(realPassword)) {
            // 跳转到成功的Servlet
            // this.getServletContext().getRequestDispatcher("/successServlet").forward(req, resp);
            resp.sendRedirect(req.getContextPath() + "/successServlet");  // 重定向
        } else {
            // 跳转到失败的Servlet
            // this.getServletContext().getRequestDispatcher("/failServlet").forward(req, resp);
            resp.sendRedirect(req.getContextPath() + "/failServlet");  // 重定向
        }
    }
}
